<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------
namespace app\Manage\controller;

use app\common\controller\Manage;
use app\common\model\FeeTemplate as FeeTemplateModel;
use app\common\model\Brand as BrandModel;
use app\common\model\DeviceCat as DeviceCatModel;
use app\common\model\DeviceType as DeviceTypeModel;
use think\facade\Request;
use think\facade\Log;
use think\Db;

class FeeTemplate extends Manage
{

    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if(Request::isAjax())
        {
            $feeTemplateModel = new FeeTemplateModel();
            return  $feeTemplateModel->tableData(input('param.'));
        }
        return $this->fetch();
    }

    /**
     *  规则添加信息
     * User:tony
     * @return array|mixed
     */
    public function add()
    {
        $feeTemplateModel = new FeeTemplateModel();
        if(Request::isPost())
        {
            return $feeTemplateModel->addData(input('param.'));
        }

        // 获取品牌列表
        $deviceCatModel = new DeviceCatModel();
        $cat_nameList = $deviceCatModel->select();

        $brandModel = new BrandModel();
        $brandModelList = $brandModel->select();

        // 使用场景列表
        $deviceTypeModel = new DeviceTypeModel();
        $type_nameList = $deviceTypeModel->select();

        return $this->fetch('add',[
            'cat_id' => $cat_nameList,
            'brand_id' => $brandModelList,
            'type_id' => $type_nameList]);
    }

    /**
     *
     *  规则信息编辑
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $feeTemplateModel = new FeeTemplateModel();
        if(Request::isPost())
        {
            return $feeTemplateModel->saveData(input('param.'));
        }
         
        $info = $feeTemplateModel->where('id',input('param.id/d'))->find();
        if (!$info)
        {
            return error_code(10002);
        }

        // 获取品牌列表
        $deviceCatModel = new DeviceCatModel();
        $cat_nameList = $deviceCatModel->select();

        $brandModel = new BrandModel();
        $brandModelList = $brandModel->select();

        // 使用场景列表
        $deviceTypeModel = new DeviceTypeModel();
        $type_nameList = $deviceTypeModel->select();

        // 获取设备参数
        
        $deviceCatModel = new DeviceCatModel();
        $brandModel = new BrandModel();

        $cat_name = $deviceCatModel->getNameById($info['cat_id']);
        $brand_name = $brandModel->getNameById($info['brand_id']);
        $type_name = $deviceTypeModel->getNameById($info['type_id']);
 
        $info['cat_name'] = $cat_name;
        $info['brand_name'] = $brand_name;
        $info['type_name'] = $type_name;

        $conf = [];
        $device = \org\Device::create($cat_name, $brand_name, $type_name, $conf);      
        $options = $device->getOptions();

        $fee_type_list = []; 
        $fee_name_list = [];
        $conf = [];
        foreach($options['data']['fee'] as $key => $val){
            $fee_type_list[] = ['name' => $val['name']];
            if($val['name'] == $info['fee_type']) {
                foreach($val['value'] as $paramKey => $paramVal){
                    $fee_name_list[] = ['name' => $paramVal['name']];
                }
            }
        }

        return $this->fetch('edit',[
            'info'=>$info,
            'cat_id' => $cat_nameList,
            'brand_id' => $brandModelList,
            'type_id' => $type_nameList,
            'fee_type' => $fee_type_list,
            'fee_name' => $fee_name_list]);
    }
 
    /**
     *
     * 规则信息删除
     * User:tony
     * @return array
     */
    public function del()
    {
        $result = [
            'status' => false,
            'msg' => '删除失败',
            'data' => []
        ];

        $id  = input('param.id/d', '');

        if (!$id) {
            return $result;
        }

        // 判断当前图片是否被使用
        $sql = "select template_id from zl_device_template_rel where template_id = '".$id."'";
    
        $list = Db::query($sql); 
        if (!$list || count($list) == 0) { 
            $feeTemplateModel = new FeeTemplateModel();
            if ($feeTemplateModel::destroy($id))
            {
                $result['status'] = true;
                $result['msg'] = '删除成功';
            }
        } else { 
            $result['msg']    = '当前计费规则已使用无法删除！';
        }

        return $result;
    }

    /**
     *
     * 获取设备参数
     * User:tony
     * @return array
     */
    public function getFeeTemplateOptions()
    {
        $feeTemplateModel = new FeeTemplateModel();
        return $feeTemplateModel->getFeeTemplateOptions();
    }
    
}
